package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

/**
 * @author 吴
 * @version V1.0
 * @Description 会员服务
 * @Package com.itheima.service.Impl
 * @date 2021/11/27 16:51
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    /***
     * 根据手机号查询用户
     * @param telephone 手机号
     * @return
     */
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.selectByTelephone(telephone);
    }

    /**
     * 保存用户
     * @param member  用户信息
     */
    @Override
    public void saveInfo(Member member) {
        String password = member.getPassword();
        if(!password.isEmpty()){
            // 加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.addMember(member);
    }

    /**
     * 根据月份日期查询之前每月月份的会员人数
     * @param beforeDates  之前12个月的日期 格式：2021-11
     * @return
     */
    @Override
    public List<Integer> findMemberCountByBeforeDate(String[] beforeDates) {

        List<Integer> list = new ArrayList<>();

        Arrays.stream(beforeDates).forEach(beforeDate->{
            Integer count = memberDao.findMemberCountBeforeDate(beforeDate);
            list.add(count);

        });
        return list;
    }
    /**
     *  分页查询检查项
     * @param queryPageBean 分页参数
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Map> member = memberDao.selectByCondition(queryPageBean.getQueryString());
        PageInfo<Map> pageInfo = new PageInfo<>(member);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 保存
     * @param member
     */
    @Override
    public void save(Member member) {
        member.setRegTime(new Date());
        memberDao.addMember(member);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {
        return memberDao.selectByMemberId(id);
    }

    /**
     * 修改
     * @param member
     */
    @Override
    public void edit(Member member) {
        memberDao.update(member);
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        memberDao.deleteById(id);
    }
    /**
     * 修改
     * @param id
     */
    @Override
    public void edit1(Integer id,Integer status) {
        memberDao.update1(id,status);
    }

    @Override
    public Member findByPhoneNumber(String phoneNumber) {
        return memberDao.findByPhoneNumber(phoneNumber);
    }
}
